package com.sc.outpatientsys.service.impl;

import com.sc.outpatientsys.entity.ClinicRegister;
import com.sc.outpatientsys.mapper.ClinicRegisterMapper;
import com.sc.outpatientsys.service.IClinicRegisterService;
import com.sc.outpatientsys.service.ex.InsertException;
import com.sc.outpatientsys.service.ex.NotFindException;
import com.sc.outpatientsys.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ClinicRegisterService implements IClinicRegisterService {

    @Autowired
    private ClinicRegisterMapper clinicRegisterMapper;


    @Override
    public void insertCR(ClinicRegister clinicRegister) {
        Timestamp time = new Timestamp(new Date().getTime());
        clinicRegister.setTime_sc(time);
        Integer rows = clinicRegisterMapper.insertClinicRegister(clinicRegister);
        if(rows != 1){
            throw new InsertException("创建门诊信息中出现异常");
        }
    }

    @Override
    public ClinicRegister findByID(int cr_ID) {
        ClinicRegister rst = clinicRegisterMapper.findByID(cr_ID);
        if (rst == null){
            throw new NotFindException("未找到");
        }
        return rst;
    }

    @Override
    public List<ClinicRegister> findByST_ID(int st_ID) {
        List<ClinicRegister> rst = clinicRegisterMapper.findByST_ID(st_ID);
        if (rst == null){
            throw new NotFindException("未找到");
        }
        return rst;
    }

    @Override
    public List<ClinicRegister> findByDC_ID(int dc_ID) {
        List<ClinicRegister> rst = clinicRegisterMapper.findByDC_ID(dc_ID);
        if (rst == null){
            throw new NotFindException("未找到");
        }
        return rst;
    }

    @Override
    public List<ClinicRegister> findAll() {
        List<ClinicRegister> rst = clinicRegisterMapper.findAll();
        if (rst == null){
            throw new NotFindException("未找到");
        }
        return rst;
    }

    @Override
    public void updateState(int cr_State, int cr_ID) {
        Integer rows = clinicRegisterMapper.updateState(cr_State,cr_ID);
        if(rows != 1){
            throw new UpdateException("更新数据时出现异常");
        }

    }

    // 按照医生的id分类和天数汇总计算频次
    @Override
    public  List<Map> statistic_DC_Count_EveryDay(int dc_ID) {

        List<Map> rst = clinicRegisterMapper.statistic_DC_Count_EveryDay(dc_ID);
        if (rst == null){
            throw new NotFindException("未找到");
        }
        return rst;
    }
    // 按照学生的id分类汇总计算频次
    @Override
    public  List<Map> statistic_ST_Count() {
        List<Map> rst = clinicRegisterMapper.statistic_ST_Count();
        if (rst == null){
            throw new NotFindException("未找到");
        }
        return rst;
    }
    // 按照一定时间段内医生的工作量
    @Override
    public List<Map> statistic_DC_Count_ByTimeSc(String start,String end) {
        List<Map> rst = clinicRegisterMapper.statistic_DC_Count_ByTimeSc(start,end);
        if(rst == null){
            throw new NotFindException("未找到");
        }
        return rst;
    }

    @Override
    public List<ClinicRegister> statistic_DC_ID_TIME(int dc_ID, String time) {
        String time_day = time.split(" ")[0].trim();
        String start = time_day.concat(" 00:00:00");
        String end = time_day.concat(" 23:59:59");
        List<ClinicRegister> rst = clinicRegisterMapper.statistic_DC_ID_TIME(dc_ID, start, end);
        if(rst.isEmpty()){
            throw new NotFindException("未找到");
        }
        return rst;
    }


}
